package com.wpw.dream.web.utils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PageUtil {

	public static LinkedList<Integer> range(Integer len,Integer start) {
		LinkedList<Integer> out = new LinkedList<>();
		Integer end;
	
		if (start == null ) {
			start = 0;
			end = len;
		}
		
		else {
			end = start;
			start = len;
		}
	
		for (int i=start ; i<end ; i++ ) {
			out.add(i);
		}
	
		return out;
	}
	
	public static List<Integer> numbers (Integer page,Integer pages) {
		LinkedList<Integer> numbers = new LinkedList<>();
		Integer	buttons = 7;
		Integer	half = buttons / 2;
	
		if (pages <= buttons ) {
			numbers = range( 0, pages );
		}
		else if ( page <= half ) {
			numbers = range( 0, buttons-2 );
			numbers.add(-1000);
			numbers.add( pages-1 );
		}
		else if ( page >= pages - 1 - half ) {
			numbers = range( pages-(buttons-2), pages );
			numbers.addFirst(-1000 ); //向头放
			numbers.addFirst(0 );
		}
		else {
			numbers = range( page-1, page+2 );
			numbers.add( -1000 );
			numbers.add( pages-1 );
			numbers.addFirst(-1000 );
			numbers.addFirst(0 );
		}
		List<Integer> res = new ArrayList<>();
		for (Integer integer : numbers) {
			res.add(integer+1);
		}
		return res;
	}
}
